<cfcomponent output="false">

<cfset variables.readers = structNew()>

<cffunction name="init" access="public" returnType="filereader" output="false" hint="Loads readers.">
	<cfset var path = expandPath("./readers")>
	<cfset var files = "">
	<cfset var ins = "">
	<cfset var md = "">
	
	<cfdirectory action="list" name="files" directory="#path#">
	<!--- get all CFCs not reader.cfc --->
	<cfquery name="files" dbtype="query">
	select	name
	from	files
	where	lower(name) like '%.cfc'
	and		name != 'reader.cfc'
	</cfquery>

	<cfloop query="files">	
		<!--- make an instance --->
		<cfset ins = createObject("component", "readers." & replace(name,".cfc", ""))>
		<cfset md = getMetaData(ins)>
		
		<cfif structKeyExists(md, "extensions") and len(md.extensions)>
			<cfset variables.readers[md.extensions] = ins>
		</cfif>
		
	</cfloop>
	
	<cfreturn this>
</cffunction>

<cffunction name="read" access="public" returnType="string" output="true" hint="Core read feature.">
	<cfargument name="file" type="string" required="true" hint="Full path to file to read.">
	<cfset var extension = "">
	<cfset var result = "">
	
	<cfif not fileExists(arguments.file)>
		<cfthrow message="#arguments.file# does not exist.">
	</cfif>
	
	<cfset extension = listLast(arguments.file, ".")>
	
	<!--- see if we have a reader for the extension --->
	<cfloop item="ext" collection="#variables.readers#">
		<cfif listFindNoCase(ext, extension)>
			<cfinvoke component="#variables.readers[ext]#" method="read" file="#arguments.file#" returnVariable="result">
			<cfreturn result>
		</cfif>
	</cfloop>

	<cfreturn "">
</cffunction>

</cfcomponent>